How the game decides two things touch - a rectangle around each instance - and the two ways to use that answer, the Collision event and the overlap functions.
Collision is the game answering "are these two things touching?" There are two ways to use that answer, and they fit different jobs.
pos_free, which answers yes or no for a place you name, and you decide what to do with the answer.Both use the same idea of touching, described next.
Every instance has a collision box: a rectangle around it. Two instances touch when their boxes overlap. The box is a rectangle, never the exact shape of the picture, so a round object touches a neighbor a little before its curve does.
x and y. Those two numbers are the middle of the instance, not a corner.mask_w and mask_h set the width and height in pixels, and can be bigger or smaller than the picture. mask_x and mask_y slide the box away from the middle, for example down so that a tall character collides with its feet. mask_sprite borrows the size of a different sprite instead. Where these overlap, the four numbers win over mask_sprite, which wins over the instance's own sprite.box_left, box_right, box_top and box_bottom give the edges of the box as it is now, in room coordinates.x and y (moved by mask_x and mask_y). The questions that compare two boxes (collision_rect, pos_meeting, pos_free and collides) treat that point like this:- A box meets the point only when the point is strictly inside it: past its left and top edges and short of its right and bottom edges. A point exactly on an edge or on a corner is not met. - Two boxes with no size never meet, even in the same spot. - A box with a width and no height (or the reverse) is a line. It is met when it lies strictly between the edges of the other box across its empty side, and overlaps the usual way along the other side. - A solid instance with no size can block: pos_free is false for a box that has its point strictly inside, and for an instance with no size whose own point is strictly inside a solid box.
The questions about a point or a line - collision_point, point_meeting, pos_solid, collision_line and collision_circle - never find a box with no size. Neither does a Collision event, which skips any instance with no width or no height, on either side. tile_free calls an instance with no size free everywhere. A controller object that only holds variables is still out of the way of every question that names another object.
These are ordinary functions. Each answers about a place, and you call them from any event. All of them are questions about a rectangle or a point and an object type.
| Function | The question | It gives back |
|---|---|---|
pos_free(x, y, obj) | could this instance stand at (x, y) without overlapping a solid obj? | true or false |
pos_meeting(x, y, obj) | would this instance overlap any obj at (x, y)? | true or false |
pos_solid(x, y, obj) | is the point (x, y) inside a solid obj? | true or false |
point_meeting(x, y, obj) | is the point (x, y) inside any obj? | true or false |
collides(other) | does this instance's box overlap other's right now? | true or false |
collision_point(x, y, obj) | which obj is at the point (x, y)? | the first one, or nothing |
collision_rect(x1, y1, x2, y2, obj) | which obj overlaps this rectangle? | the first one, or nothing |
collision_circle(x, y, radius, obj) | which obj overlaps this circle? | the first one, or nothing |
collision_line(x1, y1, x2, y2, obj) | which obj does this line pass through? (a line of sight) | the one the line reaches first, or nothing |
pos_free, pos_meeting and collides test the instance's own box. The pos_ ones move it to the place you name first, without actually moving the instance, so pos_free(x + 4, y, obj_wall) means "if I stepped 4 pixels right, would I be inside a wall?" pos_free and pos_meeting never count the asking instance itself as something in the way.
Things to know when you use them:
solid is a switch that only two of them read. The solid variable on an instance marks it as something that stops others. pos_free and pos_solid care about it, and ignore instances that are not solid. The rest report any overlap, solid or not.obj has children, they count too. With obj_enemy as the parent of obj_slime, pos_meeting(x, y, obj_enemy) is true when a slime is in the way. The section after this one has the full list.collision_point, collision_rect, collision_circle and collision_line hand back one instance, never a list, and give nothing when there is none. To reach everything in an area, loop over the instances with with and test each one.obj. That is cheap for a handful of walls and adds up for hundreds, so avoid calling one for every instance every frame when a Collision event would do. The exception is an object whose Never moves box is ticked: for pos_free, pos_meeting, pos_solid, point_meeting, collision_point and collision_rect it is looked up in a grid instead, which makes the cost nearly the same however many copies there are (Never moves).if (pos_free(x + 4, y, obj_doc_wall)) {
x += 4; // only step right when nothing solid is there
}var crate = collision_point(x, y - 8, obj_doc_crate);
if (instance_exists(crate)) {
instance_destroy(crate); // there was a crate 8 pixels above - remove it
}An object can have a parent. Say obj_enemy is the parent of obj_slime and obj_bat (see Objects and parents). When you write the parent's name in any of the places below, the game looks at the parent's own instances and at every child's, and at the children's children too.
| You write | It covers |
|---|---|
with (obj_enemy) { ... } | every obj_enemy, every obj_slime and every obj_bat |
pos_free, pos_meeting, pos_solid or point_meeting with obj_enemy | a slime or a bat in the way counts, just like an obj_enemy |
move_slide_solid(dx, dy, obj_enemy) | a solid slime or bat blocks the slide, just like a solid obj_enemy |
collision_point, collision_rect, collision_circle or collision_line with obj_enemy | can hand back a slime or a bat |
instance_count(obj_enemy) | all of them added together |
instance_nearest(x, y, obj_enemy) or instance_furthest(x, y, obj_enemy) | the closest, or the furthest, whatever kind it is |
instance_nth(obj_enemy, n) | number n, counting the plain enemies first, then each kind in turn |
event_call(obj_enemy.user0) | every kind, each running its own version of User 0 |
a hero's "Collision with obj_enemy" event | touching an obj_enemy, a slime or a bat all run it |
It only goes down. with (obj_slime) visits slimes, and any children of slimes. It never visits the parent or the bat next to it, and instance_count(obj_bat) counts bats only. An object with no children works exactly as it always did.
One thing is different:
instance_create(x, y, obj_enemy) makes exactly one obj_enemy. It never picks a child for you.The Collision event follows the same rule. The section after next explains how.
with (obj_doc_enemy) {
hp -= 1; // every kind of enemy loses 1 health, this slime included
}
var closest = instance_nearest(x, y, obj_doc_enemy);
if (closest != nothing) {
closest.hp += 5; // the nearest one heals, whether it is a slime or a bat
}"Which enemy is nearest?" can be answered with a slime or with a bat, and the game cannot know which. So the answer is treated as an obj_enemy. Through it you can use everything obj_enemy has: its variables and its events. You can also hand it to something else, check it with instance_exists, send it an event_call, or destroy it.
What you cannot reach is something only a child has. A variable that only obj_slime declares is out of reach through an obj_enemy. To get at it, ask about slimes with with (obj_slime).
The game still does the right thing to the real one. Inside with (obj_enemy), instance_destroy(self) removes the slime or bat being visited, and a child's own version of an event is the one that runs for it.
The Collision event is the "react" tool. When you add one to an object, you pick which other object it is about, such as "Collision with obj_gem". The game then runs that code once for each instance of the gem that this instance touches, with self as this instance and other as the gem.
instance_destroy(other);In a hero's "Collision with obj_gem": each gem the hero touches is destroyed. Touch three at once and the event runs three times, once per gem.
The event page, Collision, has the full detail. Three rules are worth knowing before you build on it, because they decide what runs and when.
Every frame does two things in this order. First it finds every touching pair for every Collision event in the project. Only then does it run any of the code. Nothing your events do can change which pairs were found. If one event moves an instance away, the other events for that frame still run. This happens at step 7 of a frame, after movement and before Step End; see When each event runs.
The order is fixed, so you can count on it:
obj_coin and obj_gem runs the coin's first.If obj_a has a Collision event about obj_b, and obj_b has one about obj_a, both run for the same touch, and the one whose object is earlier in the project's list runs first. Say obj_hero comes before obj_gem: for one touch the hero's event runs, then the gem's.
That order matters when one event destroys something. An instance destroyed by an earlier event takes no more part in collisions for the rest of the frame: every event still waiting to run for it is skipped, on either side of the touch. A bullet that destroys itself in its own Collision event stops the enemy's Collision event about bullets from running for that touch, if the bullet's ran first. Put the code that must always run in the event that comes first, or destroy things in the last event.
Say obj_slime and obj_bat are children of obj_enemy (see Objects and parents). A hero's "Collision with obj_enemy" runs when the hero touches an obj_enemy, and when it touches an obj_slime, and when it touches an obj_bat. You write the code once. other is the one that was touched, and it counts as an obj_enemy, so you can use everything obj_enemy has through it. (The section above says what that means, and what you cannot reach.)
There are two different things a parent can do here, and they are easy to mix up:
| What happens | |
|---|---|
| The target is a parent | Its children are matched. "Collision with obj_enemy" also runs for slimes and bats. |
| The object that owns the event has a parent | The child does inherit the event. If obj_hero has "Collision with obj_gem" and obj_knight is a child of obj_hero, a knight touching a gem runs it, with self as the knight. |
Both can happen at once. If obj_hero has "Collision with obj_enemy" and obj_knight is a child of obj_hero, then a knight touching a slime runs the hero's code with self as the knight and other as the slime. A child that writes its own version of an event replaces the inherited one, the same as with any other event.
It only goes down, like everywhere else. "Collision with obj_slime" runs for slimes and for any children of slimes. It does not run for a plain obj_enemy, and it does not run for a bat.
#### More than one event can match
An object can have a Collision event for a parent and one for a child. Say the hero has "Collision with obj_enemy" and "Collision with obj_slime". Touching a slime runs both, once each. The slime event does not replace the enemy event. They are two events, like any two. Touching a bat runs only the enemy one.
If you want a slime to behave differently, put the slime code in the slime event and leave the enemy event for what is true of every enemy.
#### One handler, written once
The game does not copy your code once per child. The event is still a single piece of code, and each child kind is simply another place it gets called from. A new child adds a small amount of checking code to the game, never another copy of your event, and it gives the game one more pair of kinds to test (see the costs below).
instance_destroy(other) removes the real slime or bat, and a child's own Destroy event is the one that runs. with (obj_enemy) and pos_meeting(x, y, obj_enemy) work the same way, so asking the question yourself and using the event always agree about who counts as an enemy.
obj_enemy" where obj_enemy has two children is three pairs: the hero against enemies, against slimes, and against bats. Each kind keeps its own pool of instances, so each one is tested on its own. If the hero also has children, each child of the hero makes its own set of pairs too. Two families of three kinds each is nine pairs. Your code does not get longer, but the game has more pairs to check, so keep the instance limits of a big family small.Instances are not the only things that block. A room can have a tile layer: a grid of 8-pixel cells, each marked as one of three classes - pass (open), solid, or low cover (blocks a body, but a bullet flies over it). The tile functions ask about it by position:
| Function | What it does |
|---|---|
tile_free(x, y) | could this instance's whole box stand at (x, y) without touching solid ground? The tile version of pos_free |
tile_solid, tile_blocks_projectile | does the single tile at the point (x, y) stop a body, or stop a projectile |
tile_class, tile_flags | read the tile's class, or its whole attribute byte (name your own flags: see tile flags) |
tile_set_class, tile_set_flags | change what a tile blocks, for something like a dig or a door |
tile_index, tile_set_index | read or change which picture a tile shows, not what it blocks |
tile_one_way | ask whether a spot is on a One-way platform, a floor you can jump up through and stand on |
tile_shape, tile_set_shape, tile_ground_y | read or change a tile's slope shape (a ramp instead of a full block), or ask where the ground is in one column of pixels |
tile_layer_set_active, tile_layer_active | switch to one of the room's collision layers, and ask which one is on: a change you drew in the Room Editor, such as a door that opens. Only one layer is on at a time. The game remembers it |
Which one for moving? Use tile_free. Your instance has a box, and a 16 by 16 box covers four tiles at once (nine if it is not lined up with the grid). tile_free checks all of them. The point functions, tile_solid and tile_blocks_projectile, check only the single tile under one point, so the corners of a moving box can slide into a wall they never looked at. Keep the point functions for things that really are a point, like a bullet. The tile_free page has a worked example. It reads the same as the pos_free one above, with the room as the wall.
A tile does not have to be a full block. It can be a slope, and then tile_free, tile_solid and the other questions about the room ask about the pixels of its shape.
The edge of a room is not a wall. A place outside the room's own map blocks nothing, so a thing that reaches the edge keeps going and leaves the room. To keep things in, paint Solid collision along the edge, or check the position in code (room_left(), room_right(), room_top() and room_bottom() give the four edges). For tile_free that means the part of a box that is off the map blocks nothing. A room with no tile layer has nothing to block you, so every place there is free. Changes made with the tile_set_ functions apply to the current room and are lost when the room reloads. A change that should stay, like a door that opens for good, is a collision layer instead: you draw it in the Room Editor and the game remembers it.
This section is about speed. You can skip it while you are learning, and come back when your game starts to feel slow.
Both ways of asking do the same work underneath. To find out whether a hero is touching a coin, the game has to compare the hero against every coin that exists. Ten coins means ten comparisons. That is true whether a Collision event found the touch for you or you asked with pos_meeting. A Collision event is not a cleverer method; it is the same search, started for you.
The difference is how many times the search runs.
pos_free ten times, the game searches ten times.That is the whole of it, and it adds up faster than you would think. The Platformer template's player asks pos_free ten times every frame — once for sideways movement, once to test for ground, and several times while falling or rising. Each of those searches every piece of ground in the room. With forty pieces of ground that is about 328 comparisons every frame, for one player.
On a Game Boy Advance that is fine; it has time to spare. On a smaller console it is not: across the shipped projects that cannot reach full speed on a Game Boy Color, collision is between half and almost all of the busiest frame's work. It is the single most expensive thing most Gbabel games do.
var on_ground = !pos_free(x, y + 1, obj_ground); once, then use on_ground as often as you like, costs one search instead of several.tile_free. It checks your whole box against the tiles under it: about four looks for a 16 by 16 box, nine at most, and each one goes straight to the map square. That is a few looks, not a search through every instance — and it does not get slower as your level gets bigger. A platform made of a hundred ground instances is a hundred comparisons; the same platform painted as tiles is about four. (The point functions, tile_solid and friends, are one look each, but they only see one point - fine for a bullet, not for a mover.)**A question about where you are going cannot become a Collision event.** An event tells you what you are touching, and it can only do that after you have moved. Movement code needs to know before: may I step here? So pos_free(x + dx, y, obj_ground) has no event version, and that is not a gap in Gbabel — the two are different questions.
This is why the Platformer template asks ten times instead of using an event: every one of its questions is about a place the player has not moved to yet. The way to make that code cheaper is option 2 or 3 above, not an event.
Sometimes you want what a Collision event gives you, but written in your own code — because the check has to happen at an exact moment, or because you want the reaction to live on the other object. You can do it, and it is worth seeing how, because it shows what the event was doing for you all along.
Two pieces make it work.
with runs your code inside another object's instances. with (obj_bullet) { ... } runs the braces once for every bullet that exists. Inside them, self is that bullet — not the object that started the loop. The one that started it becomes other.
collides compares two instances you name. collides(other) is true when self's box overlaps other's. It only ever looks at those two, so it does no searching of its own.
Together, in an enemy's Step End event:
with (obj_bullet) {
if (collides(other)) {
other.hp -= dmg;
instance_destroy(self);
}
}Read it as: for every bullet, if that bullet is touching me, take its damage off my health and remove it. Inside the braces self is the bullet and other is the enemy — so dmg is the bullet's own damage and other.hp is the enemy's health. That is the same thing a "Collision with obj_bullet" event on the enemy does, with self and other swapped round.
Use Step End, not Step. Instances move after Step and before Step End. In Step you would be testing where the bullets were last frame, and a fast one could appear to pass straight through.
other only means something inside the braces. Outside a with block or a Collision event there is no other instance to point at, so the name has nothing to stand for. Keep the whole check inside the braces.
with reaches children too. with (obj_bullet) visits obj_bullet and every object that is its child, so one loop covers every kind of bullet. A Collision event does the same: a "Collision with obj_bullet" runs for every kind of bullet. If the check lives in the enemies, put it on the parent obj_enemy and every kind of enemy gets it.
It does not make the searching cheaper. Every bullet is still compared with every enemy. That work is the same whichever way round you write it, and it is most of the cost.
What changes is how many times a search is started. Ten bullets each asking about two kinds of enemy starts twenty searches. The enemies asking instead starts one each. When bullets heavily outnumber enemies — a rapid-fire weapon against a few targets — that is a real saving, and this is how you get it.
It also catches something the single-answer functions miss. pos_meeting tells you whether something is there and collision_point hands you one of them. If two bullets hit the same enemy in one frame, the second is never seen, and it is not removed either, so it sails on through. The with loop visits every bullet, so both land. A real Collision event behaves the same way: it runs once for each touching pair.
A real Collision event is still less to write. Because of the inheritance rule above, putting it on a parent obj_enemy means every kind of enemy gets it from one place. Write the version on this page when you need to control exactly when the check happens; use the event the rest of the time.
tile_free for anything that moves, tile_solid for a single point.